package com.woparker.kiosk.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.woparker.kiosk.R;
import com.woparker.kiosk.base.ContextHolder;
import com.woparker.kiosk.utils.CommonUtils;


public class PathAnimationView extends View {
    private Path drawPath;
    private Paint paint;
    private float[] pos = new float[2];
    private float[] tan = new float[2];
    private ValueAnimator animator;

    public PathAnimationView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void init(Path rootPath, Bitmap bgBitmap, Canvas canvas) {
        // 初始化画笔
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(8);
        paint.setStyle(Paint.Style.STROKE);
        canvas.getHeight();
        canvas.getWidth();
        Log.e("PathAnimationView", "ca width:" + canvas.getWidth() + ", height:" + canvas.getHeight() + ", viewWidth:" + getWidth() + ", viewHeight:" + getHeight());
        float bottomHeight = getContext().getResources().getDimensionPixelSize(R.dimen.dp_120);
        float statusBarHeight = CommonUtils.getStatusBarHeight(ContextHolder.getAppContext());
        int[] size = CommonUtils.getScreenSize();
        float width = size[0];
        float height = size[1] + statusBarHeight;
        float bitWidth = bgBitmap.getWidth();
        float bitHeight = bgBitmap.getHeight();
        //放大比例
        float scalseSize = (height - bottomHeight) / bitHeight;
        Log.e("PathAnimationView", "width:" + width + ", height:" + height + ", " +
                "bitWidth:" + bitWidth + ", bitHeight:" + bitHeight + ",bottomHeight:" + bottomHeight + "," +
                "scalseSize:" + scalseSize + ",statusBarHeight:" + statusBarHeight);
        float translateX = (width - bgBitmap.getWidth() * scalseSize) / 2;
        // 创建一个新的路径进行平移
        drawPath = new Path();
        rootPath = scalePath(rootPath, scalseSize, scalseSize);
        rootPath.offset(translateX, 0, drawPath);
        // 创建动画
        animator = ValueAnimator.ofFloat(0, 1);
        animator.setDuration(5000); // 动画持续时间
        animator.setRepeatCount(ValueAnimator.INFINITE); // 无限循环
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                PathMeasure measure = new PathMeasure(drawPath, false);
                measure.getPosTan(measure.getLength() * value, pos, tan);
                invalidate(); // 刷新视图
            }
        });
    }

    public static Path scalePath(Path path, float scaleX, float scaleY) {
        // 创建一个 Matrix 对象
        Matrix matrix = new Matrix();
        // 设置缩放比例
        matrix.setScale(scaleX, scaleY);
        // 创建一个新的 Path 对象来存储放大后的路径
        Path scaledPath = new Path();
        // 将原始 Path 对象应用缩放变换
        path.transform(matrix, scaledPath);
        return scaledPath;
    }


    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        // 绘制移动对象
        if (null != animator && animator.isRunning()) {
            canvas.drawCircle(pos[0], pos[1], 5, paint);
        }
    }

    public void startAnimation() {
        animator.start();
    }

}

